package com.xc.h05.service.wutao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.*;
import com.xc.h05.entity.Specialty;
import com.xc.h05.mapper.ConsultMapper;
import com.xc.h05.mapper.ConsultSpecialtyMapper;
import com.xc.h05.mapper.Consultlevel2Mapper;
import com.xc.h05.mapper.SpecialtyMapper;
import com.xc.h05.service.wutao.ConsultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ConsultServiceImpl implements ConsultService {
    @Autowired
    private ConsultMapper consultMapper;
    @Autowired
    private SpecialtyMapper specialtyMapper;
    @Autowired
    private ConsultSpecialtyMapper consultSpecialtyMapper;
    @Autowired
    private Consultlevel2Mapper consultlevel2Mapper;


    @Override
    public int add(Consult consult) {
        int add = consultMapper.add(consult);
        return add;
    }

    @Override
    public int delete(int id) {
        int delete = consultMapper.delete(id);
        return delete;
    }

    @Override
    public int update(Consult consult) {
        int update = consultMapper.update(consult);
        return update;
    }

    @Override
    public List<Consult> findAll() {
        List<Consult> list = consultMapper.findAll();
        return list;
    }

    @Override
    public PageInfo<Consult> findByPage(int pageNum, int pageSize, Consult consult) {
        PageHelper.startPage(pageNum,pageSize);
        List<Consult> list = consultMapper.findByPage(consult);
        for(Consult ca:list){
            Consultlevel consultlevel = consultlevel2Mapper.findById( Math.toIntExact ( ca.getLevelId ( ) ) );
            ca.setConsultlevel ( consultlevel );
        }
        PageInfo<Consult> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }



    @Override
    public int deleteByIds(List<Long> ids) {
        int i = consultMapper.deleteByIds(ids);
        return i;
    }
    @Override
    public Consult findById(Integer id) {
        Consult consult = consultMapper.findById(id);
        return consult;
    }

    @Override
    public List<Consult> findByCid(int cid,List<Integer> types) {
        List<Consult> list;
        //types 参数是擅长领域集
        if (cid == 0) {
            list = consultMapper.findAll();
        }else {
            //得到该等级下的所有咨询师
            list = consultMapper.findByCid(cid);
        }

        //得到对应的擅长类型领域集
        List<Specialty> typeList = specialtyMapper.findByIds(types);

        for (Consult consult : list) {
            Consultlevel consultlevel = consultlevel2Mapper.findById(Math.toIntExact(consult.getLevelId()));
            consult.setConsultlevel(consultlevel);
        }
//        specialtyMapper.findAllByName()
        for (Consult consult : list) {
            List<Integer> list1 = consultSpecialtyMapper.findByCid(consult.getId());
            if(list1.size()==0){
                consult.setSpecialtyList(null);
                continue;
            }
            //获取每个咨询师对应的擅长领域
            List<Specialty> list2 = specialtyMapper.findByIds(list1);
            consult.setSpecialtyList(list2);
        }
        List<Consult> list3 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSpecialtyList()!=null && list.get(i).getSpecialtyList().containsAll(typeList)) {
                list3.add(list.get(i));
            }
        }
        return list3;
    }
    // 根据擅长领域id查询对应的咨询师
    @Override
    public PageInfo<Consult> findBySpecialtyId(int pageNum,int pageSize, int id) {
        PageHelper.startPage(pageNum,pageSize);
        List<Consult> list = consultMapper.findBySpecialtyId(id);
        for(Consult ca:list){
            Consultlevel consultlevel = consultlevel2Mapper.findById( Math.toIntExact ( ca.getLevelId ( ) ) );
            ca.setConsultlevel ( consultlevel );
        }
        PageInfo<Consult> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
   public List<Consult> ConslutWithSpecialty() {
        List<Consult> list = consultMapper.findByPageWithSpecialty();
        for(Consult ca:list){
            List<Integer> consultSpecialty = consultSpecialtyMapper.findById(ca.getId());
            System.out.println (consultSpecialty );
            if (consultSpecialty.size () == 0) {
                ca.setSpecialtys ( null );
                continue;
            }
             List<Specialty> specialty = specialtyMapper.findById(consultSpecialty);
             ca.setSpecialtys ( specialty );
        }
        return list;
    }

}
